﻿using blackhouse.NemoBlog.Business.Model;
using blackhouse.NemoBlog.Business.View;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.NemoBlog.Business {
    public class NemoBlogConfiguration : GroupRecord {

        #region Methods

        public IBlogViewFactory GetViewFactory() {
            return this.LoadDefinedType("viewfactory") as IBlogViewFactory;
        }

        public IPostListModel GetPostListModel() {
            IPostListModel postListModel = this.LoadDefinedType("postlistmodel") as IPostListModel;
            if (postListModel == null)
                postListModel = new StandardPostListModel();
            return postListModel;
        }

        public IPostModel GetPostModel(int postId) {
            IPostModel postModel = this.LoadDefinedType("postmodel", postId) as IPostModel;
            if (postModel == null)
                postModel = new StandardPostModel(postId);
            return postModel;
        }

        public int[] GetEditorHideColumns() {
            GroupRecord[] records = this.GetRecords("ukryj_pola");
            if (records.Length > 0)
                return records.Select(r => r.RecordId).ToArray();
            else
                return new int[0];
        }

        private object LoadDefinedType(string column, params object[] constructorArgs) {
            string sType = this.GetString(column);
            if (String.IsNullOrEmpty(sType)) return null;
            Type type = Type.GetType(sType, false);
            if (type == null) return null;
            try {
                return Activator.CreateInstance(type, constructorArgs);
            }
            catch (Exception) {
                return null;
            }
        }

        #endregion

    }
}
